<?php
class IndexController extends Sosweb_Controller_Action
{
	function init()
	{
		parent::init();
		$this->logger->info('Inicjalizacja strony domowej profitmania.pl przez uzytkownika z IP: '.$_SERVER['REMOTE_ADDR']);
		
		$this->_helper->layout()->getView()->headTitle('ProfitMania.pl - Zarabiaj na zakupach - '.Sosweb_Const_DescriptionConst::$TITLE_1, 'SET');
		$this->_helper->layout()->getView()->getHelper('HeadMeta')->appendName('description', Sosweb_Const_DescriptionConst::$DESC_1);		
	}

	function indexAction()
	{
	    try
       	{
			//przygotowanie danych najgoretszych promocji		
			$forum_threads_rating_view = new ForumThreadsRatingView();
	    	$select_rating = $forum_threads_rating_view->select();
			$select_rating->limit(Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE);
				
			$forum_threads_replies_view = new ForumThreadsRepliesView();
	    	$select_replies = $forum_threads_replies_view->select();
			$select_replies->limit(Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE);
	
	    	$forum_threads_views_view = new ForumThreadsViewsView();
	    	$select_views = $forum_threads_views_view->select();
	    	$select_views->limit(Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE);
	    	
	    	$forum_threads_report_view = new ForumThreadsReportView();
	    	$select_report = $forum_threads_report_view->select();
			$select_report->limit(Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE);
			
	    	//przygotowanie danych o najnowszych kuponach
			$coupons_view = new CouponsView();
	    	$select_coupons = $coupons_view->select();
			$select_coupons->limit(Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE)
				->order('code_views DESC')
				->order('title_views DESC')
				->order('posted_date DESC');

			//pobieranie danych	
       		$this->logger->info('Fetching threads data for rating criterium. '.$select_rating->assemble());
			$ratings = $forum_threads_rating_view->fetchAll($select_rating);
			
			if(count($ratings)== 0)
			{
				$this->logger->warn('Nie znaleziono informacji o ratingach promocji.');
			}
			
       		$this->logger->info('Fetching threads data for replies criterium. '.$select_replies->assemble());
			$replies = $forum_threads_replies_view->fetchAll($select_replies);
			
			if(count($replies)== 0)
			{
				$this->logger->warn('Nie znaleziono informacji o komentarzach promocji.');
			}
			
       		$this->logger->info('Fetching threads data for views criterium. '.$select_views->assemble());
			$views = $forum_threads_views_view->fetchAll($select_views);
			
			if(count($views)== 0)
			{
				$this->logger->warn('Nie znaleziono informacji o odczytaniach promocji.');
			}

       		$this->logger->info('Fetching threads data for report criterium. '.$select_report->assemble());
			$reports = $forum_threads_report_view->fetchAll($select_report);
			
			if(count($reports)== 0)
			{
				$this->logger->warn('Nie znaleziono informacji o zgłoszeniach promocji.');
			}
			
			$fpr = new Sosweb_Db_Procedures_FirstPageRanking($ratings, $replies, $views, $reports);
			$hot_promotions = $fpr->getRanking();
			
       		$this->logger->info('Fetching newest coupon data . '.$select_coupons->assemble());
			$newest_coupons = $forum_threads_report_view->fetchAll($select_coupons);
			
			$this->view->newest_coupons = $newest_coupons;
			$this->view->hot_promotions = $hot_promotions;
       	}
		catch(Zend_Exception $ex)
		{
			$this->logger->error($ex->getMessage());
			$this->view->message = 'Wystąpił błąd aplikacji podczas pobierania danych o promocjach i kuponach.';
			
			return;			
		}		
	}
}